{
  //  根据key 谁比谁少了
  console.time("arr")
  var arr = [];
  for (var i = 0; i <= 5; i++) {
    arr.push({
      name: i
    })
  }
  var del = [{
    name: 1
  }, {
    name: 3
  }, {
    name: 11
  }]


  const differenceBy = (a, b, fn) => {
    const s = new Set(a.map(fn));
    return b.filter(x => !s.has(fn(x)));
  };
  var a = differenceBy(arr, del, v => v.name); //arr比del 少了的数
  var b = differenceBy(del, arr, v => v.name); //del 比 arr 少了的数
  console.log("arr比del 差的数", a)
  console.log("del 比 arr差的", b)
  console.timeEnd("arr")

}





{
  //完全去重 重复的都不要了
  var arr = [];
  for (var i = 0; i <= 5; i++) {
    arr.push({
      name: i
    })
  }
  var del = [{
    name: 1
  }, {
    name: 3,
  }, {
    name: 11
  }]

  let diffrence = function (a, b) {
    var a = a.map(JSON.stringify);
    var b = b.map(JSON.stringify);
    return a
      .concat(b)
      .filter(v => !a.includes(v) || !b.includes(v))
      .map(JSON.parse);
  }
  console.log(diffrence(arr, del))

}


{
  console.time("arr")
  //  交集 根据Key 获取交集
  var arr = [];
  for (var i = 0; i <= 5; i++) {
    arr.push({
      name: i
    })
  }
  var del = [{
    name: 1,
    sel: 1
  }, {
    name: 3
  }, {
    name: 11
  }]

  const differenceBy = (a, b, fn) => {
    const s = new Set(b.map(fn));
    return a.filter(x => s.has(fn(x)));
  };
  var a = differenceBy(arr, del, v => v.name); //arr和del 的相同  [ { name: 1 }, { name: 3 } ]
  console.log(a)
  console.log("---------")
  var b = differenceBy(del, arr, v => v.name); //del和arr 的相同 [ { name: 1, sel: 1 }, { name: 3 } ]
  console.log(b)
  console.timeEnd("arr")

}





{
  //根据key 去重 保留第一个重复
  console.time("reduce")
  var arr = [];
  for (var i = 0; i <= 100000; i++) {
    arr.push({
      id: i
    })
  }
  const id = "id" //这里定义按照过滤的对象的属性名称
  const newArr = arr.reduce((all, next) => all.some((item) => item[id] == next[id]) ? all : [...all, next], []);
  console.timeEnd("reduce")
}





{
  //完全去重 保留第一个重复
  var arr = [];
  for (var i = 0; i <= 5; i++) {
    arr.push([i, i])
  }
  var del = [
    [0, 0],
    [1, 1]
  ]
  let diffAll = function (a, b) {
    return Array.from(new Set([...a.map(JSON.stringify), ...b.map(JSON.stringify)])).map(JSON.parse)

  }
  console.log(diffAll(arr, del))
} {
  //完全去重 
  console.time("time")
  var arr = [];
  var obj = {};
  for (var i = 0; i <= 1000000; i++) {
    arr.push({
      key: i
    })
    if (i % 2 == 0) {
      obj[i] = true
    }
  }
  var result = [];

  for (var i = 0; i < arr.length; i++) {
    if (!obj[arr[i].key]) {
      result.push(arr[i]);
      obj[arr[i].key] = true;
    }
  }
  console.log(result)
  console.timeEnd("time")
}


{ //演示
  let a = []
  for (let i = 0; i < 10000000; i++) {
    a.push(i)
  }








  let dd = []
  console.time("for")
  for (let i = 0; i < a.length; i++) {
    dd.push(i)
  
  }
  console.timeEnd("for")
  console.time("filter")
  let cc = a.filter((v,i) => { return v })
  console.timeEnd("filter")

}